'use strict'

class collectjoke {
  constructor() {
    this.param = '/'
  }

  async get(c) {
    let user_id = c.box.user.openid,
      ret = await c.service.psql.query(`select * from collectjoke where collectjoke_user_id like '${user_id}' order by collectjoke_id asc`),
      resultArr = ret.rows,
      respondArr = []
    for (let i = 0; i < resultArr.length; i++) {
      var temp = await c.service.psql.query(`select * from joke where joke_id like '${resultArr[i].collectjoke_id}'`)
      respondArr.push(temp.rows[0])
    }
    c.status(200).send(respondArr)
  }

  async post(c) {
    var user_id = c.box.user.openid,
      obj = c.body,
      joke_id = obj.id
    let ret2 = await c.service.psql.query(`select * from joke where joke_id like '${joke_id}'`)
    if (!ret2.rows[0]) {
      c.status(400).send('joke_id 有误 :)')
      return
    }
    let ret = await c.service.psql.query(`select * from collectjoke where collectjoke_id like '${joke_id}' and collectjoke_user_id like '${user_id}'`)
    if (ret.rows[0]) {
      ret = await c.service.psql.query(`delete from collectjoke where collectjoke_id like '${joke_id}' and collectjoke_user_id like '${user_id}'`)
      c.status(200).send('unlike success')
    } else {
      ret = await c.service.psql.query(`insert into collectjoke values('${joke_id}', '${user_id}')`)
      c.status(200).send('like success')
    }
    console.log(ret)
  }

}

module.exports = collectjoke